using System;
using System.Web.Mvc;

using Azure.Infrastructure;
using Azure.Web.Models;
using Azure.Domain.Messages;
using Azure.Domain.Models;
using Azure.Domain.Services;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace Azure.Web.Controllers
{
    [Authorize]
    public class AdminController : BaseController
    {
        private readonly IBlogService _blogService;
        private readonly IBlogConfiguration _configuration;

        public AdminController(IBlogService blogService, IBlogConfiguration configuration) : base(configuration)
        {
            _blogService = blogService;
            _configuration = configuration;
        }

        [ActionName(Admin.Index)]
        public ActionResult Index()
        {
            return View("Index");
        }

        [ActionName(Admin.Create)]
        public ActionResult Create()
        {
            return View("Create");
        }

        [ActionName(Admin.Create)]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CreateViewModel model)
        {
            var message = new NewPostMessage
                              {
                                  Author = User.Identity.Name,
                                  Body = model.Body,
                                  Title = model.Title,
                                  Slug = model.Slug,
                                  Created = DateTime.UtcNow
                              };
            _blogService.Create(message);

            return RedirectToAction(Admin.Index);
        }

        [ActionName(Admin.List)]
        public ActionResult List()
        {
            var items = _blogService.Last(5);
            return View(new PostListViewModel(items));
        }

        [ActionName(Admin.Edit)]
        public ActionResult Edit(string partitionKey, string rowKey)
        {
            var post = _blogService.LoadPost(partitionKey, rowKey);

            return View(new EditPostViewModel(post));
        }

        [ActionName(Admin.Edit)]
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(UpdatePostMessage updatePostMessage)
        {
            _blogService.UpdatePost(updatePostMessage);
            return RedirectToAction(Admin.Index);
        }

        [ActionName(Admin.Delete)]
        public ActionResult Delete()
        {
            var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            var client = storageAccount.CreateCloudTableClient();
            client.DeleteTableIfExist(For<Post>.Table);
            client.DeleteTableIfExist(For<User>.Table);

            return Redirect("~/");
        }

        [ActionName(Admin.Configuration)]
        public ActionResult ViewConfiguration()
        {
            return View(_configuration.Configuration);
        }

        [ActionName(Admin.Configuration)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditConfiguration(SiteConfiguration configuration)
        {
            _configuration.Save(configuration);

            return RedirectToAction(Admin.Index);
        }
    }

    public static class Admin
    {
        public const string Delete = "delete";
        public const string Index = "index";
        public const string List = "posts";
        public const string Create = "new";
        public const string Controller = "Admin";
        public const string Edit = "Edit";
        public const string Configuration = "Configuration";
    }
}